<?php
/**
 * ActivityModel.class.php
 * 活动模型
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-12
 */

namespace Admin\Model;

class ActivityModel extends BaseModel {
	//重定义数据表名称
	protected 	$trueTableName 	= 't_activity';

	/**
	 * 添加条目
	 * @param 	$title
	 * @param 	$banner
	 * @param 	$audio
	 * @param 	$audioTime
	 * @param 	$introduct
	 * @param 	$content
	 * @param 	$iHeader
	 * @param 	$iAudio
	 * @param 	$iAudioTime
	 * @param 	$iFooter
	 * @param 	$iiCont
	 * @param 	$iiiCont
	 * @param 	$level
	 * @return 	$activityId
	 */
	function addItem($title, $banner, $audio, $audioTime, $introduct, $content, $iHeader, $iAudio, 
		$iAudioTime, $iFooter, $iiCont, $iiiCont, $level){
		$activityInfo 	= [
			'title' 	=> $title,
			'banner'	=> $banner,
			'audio'		=> $audio,
			'audio_time'=> $audioTime,
			'introduct'	=> $introduct,
			'content'	=> $content,
			'i_header'	=> $iHeader,
			'i_audio'	=> $iAudio,
			'i_audio_time'	=> $iAudioTime,
			'i_footer'	=> $iFooter,
			'ii_cont'	=> $iiCont,
			'iii_cont'	=> $iiiCont,
			'level'	=> $level,
			'create_time'	=> time(),
			'update_time'	=> time(),
			'status'	=> 1
		];

		$activityId 	= $this->add($activityInfo);
		if($activityId === false){
			$this->errMsg 	= '活动信息添加失败!';
			return false;
		}

		return $activityId;
	}

	/**
	 * 获取列表
	 * @param 	$level
	 * @param 	$page
	 * @param 	&$pageHtml
	 * @return 	$list
	 */
	function getList($level, $page = 1, &$pageHtml){
		$where 	= 'status != -1';
		if($level && is_numeric($level)){
			$where 	.= ' and level = '.$level;
		}

		$list 	= $this->where($where)
			->page($page, C('PAGE_COUNT'))
			->order('id desc')
			->field('id, title, banner, create_time, update_time, level, status')
			->select();
		$list 	= $this->_analyField('activity', $list, true);

		if($pageHtml === true){
			$count 	= $this->where($where)->cache(true)->count();
			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		return $list;
	}

	/**
	 * 获取详情
	 * @param 	$activityId
	 * @return 	$activityInfo
	 */
	function getItem($activityId){
		$activityInfo 	= $this->where('id = %d and status != -9', $activityId)->find();
		if(!$activityId){
			$this->errMsg 	= '未找到相关活动!';
			return false;
		}

		$activityInfo 	= $this->_analyField('activity', $activityInfo);
		return $activityInfo;
	}

	/**
	 * 更改信息
	 * @param 	$activityId
	 * @param 	$activityInfo
	 * @return 	boolean
	 */
	function updateItem($activityId, $activityInfo){
		$result 	= $this->where('id = %d', $activityId)->save($activityInfo);
		if($result === false){
			$this->errMsg 	= '活动信息更改失败!';
			return false;
		}

		return true;
	}

	/**
	 * 获取活动须知列表
	 * @param 	$activityId
	 * @return 	$list
	 */
	function getKnownList($activityId){
		$list 	= $this->table('t_activity_known as a')
			->join('left join t_activity b on a.activity_id = b.id')
			->where('a.activity_id = %d and a.status != -1', $activityId)
			->order('id desc')
			->field('a.id, a.activity_id, a.title, a.audio, a.audio_time, a.create_time, 
				a.update_time, a.status, b.title as activity_title')
			->select();

		$list = $this->_analyField('activityknown', $list, true);

		return $list;
	}

	/**
	 * 添加活动须知
	 * @param 	$activityId
	 * @param 	$title
	 * @param 	$audio
	 * @param 	$audioTime
	 * @return 	$knownId
	 */
	function addKnown($activityId, $title, $audio, $audioTime){
		$known 	= [
			'activity_id' 	=> $activityId,
			'title'	=> $title,
			'audio'	=> $audio,
			'audio_time'	=> $audioTime,
			'create_time'	=> time(),
			'update_time'	=> time(),
			'status'	=> 1
		];
		
		$knownId 	= M('t_activity_known', null)->add($known);
		if($knownId === false){
			$this->errMsg 	= '活动须知添加失败!';
			return false;
		}
		return $knownId;
	}

	/**
	 * 变更活动须知
	 * @param 	$knownId
	 * @param 	$knownInfo
	 * @return 	boolean
	 */
	function updateKnown($knownId, $knownInfo){
		$knownInfo['update_time']	= time();
		$result	= M('t_activity_known', null)->where('id = %d', $knownId)->save($knownInfo);
		if($result === false){
			$this->errMsg 	= '活动须知变更失败!';
			return false;
		}

		return true;
	}

	/**
	 * 获取相册列表
	 * @param 	$keyword 	关键词
	 * @param 	$timeS 	开始时间
	 * @param 	$timeE 	结束时间
	 * @param 	$page 	页码数
	 * @param 	&$pageHtml 	页码HTML
	 * @return 	$list
	 */
	function getPictureList($keyword, $timeS, $timeE, $page, &$pageHtml){
		$where 	= 'a.status != -1';
		if($timeS){
			$where 	.= ' and a.create_time >= '.$timeS;
		}
		if($timeE){
			$where 	.= ' and a.create_time <= '.$timeE;
		}
		if($keyword && strstr($keyword, ':')){
			$keyword 	= explode(':', $keyword);
			switch($keyword[0]){
				case '活动':
					$where 	.= sprintf(' and b.title like "%s%%"', $keyword[1]);
				break;
				case '用户':
					$where 	.= sprintf(' and (c.nickname like "%s%%" or c.truename like "%s%%")', $keyword[1], $keyword[1]);
				break;
			}
		}

		$list 	= M('t_activity_picture as a', null)
			->join('left join t_activity as b on a.activity_id = b.id')
			->join('left join t_user as c on a.user_id = c.id')
			->where($where)
			->order('a.id desc')
			->page($page, C('PAGE_COUNT'))
			->cache(true)
			->field('a.id, a.user_id, a.activity_id, a.path, a.beautify_path, a.origin,
				a.create_time, b.title activity_title, c.nickname, c.truename')
			->select();
		$list 	= $this->_analyField('activity_picture', $list, true);

		if($pageHtml === true){
			$count 	= M('t_activity_picture as a', null)
				->join('left join t_activity as b on a.activity_id = b.id')
				->join('left join t_user as c on a.user_id = c.id')
				->where($where)
				->cache(true)
				->count();
			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		return $list;
	}
}